/* (c) 2013 Andreas Kasch */
Tools = {};

H5.Pages.Creator = new function(){

	this.options = {
		addToHistory: true,
		clearHistory: false
	}
	
	
	this.preload = function(args, callback){
	
		H5.includeScript("script/wgc.js?v=" + H5.getRandom(), function(){
			
			WGC.init();
			
			H5.Storage.set("dataCreated", "true");
			callback({
				random: H5.getRandom(),
				newBox: H5.t("Creator_newBox"),
				editBox: H5.t("Creator_editBox"),
				removeBox: H5.t("Creator_removeBox"),
				newEntity: H5.t("Creator_newEntity"),
				editEntity: H5.t("Creator_editEntity"),
				removeEntity: H5.t("Creator_removeEntity"),
				scenegraphBgImage: H5.t("Creator_left_scenegraph_bgImage"),
				scenegraphBackground: H5.t("Creator_left_scenegraph_background"),
				scenegraphCenter: H5.t("Creator_left_scenegraph_center"),
				scenegraphForeground: H5.t("Creator_left_scenegraph_foreground"),
				scenegraphGrid: H5.t("Creator_left_scenegraph_grid"),
			}, null);

			
			
		});
	
		
	};

	this.load = function(){
	
	
		enableSidebarRescale();
		loadScenegraph();
		
		loadEditCanvas();
		
		updateGamestate();
		Tools.selectedBox = -1;
		Tools.selectedEntity = -1;
		Tools.selectedLayer = WGC.data.game.centerLayerId;
		Tools.visibility = {};
		Tools.visibility.bgImage = 2;
		Tools.visibility.background = 2;
		Tools.visibility.center = 2;
		Tools.visibility.foreground = 2;
		Tools.visibility.grid = 2;
		
			
		$("*").on("drop", function(evt){
			evt.stopPropagation();
			evt.preventDefault();
		});
		$("*").on("dragover", function(evt){
			evt.stopPropagation();
			evt.preventDefault();
		});
		
		$(document).on("click", "#Creator_tabBar_start", function(){
			EditCanvas.stop();
			loadPlayCanvas(false);
		});
		
		$(document).on("click", "#Creator_tabBar_debug", function(){
			EditCanvas.stop();
			loadPlayCanvas(true);
		});
		
		$(document).on("click", "#Creator_tabBar_stop", function(){
			PlayCanvas.stop();
			loadEditCanvas();
		});
		
		$("#Creator_sidebar_statusbar_newBox").on("click", function(){
			loadSpritesheetUpload(false);
			EditCanvas.stop();
		});
		
		$("#Creator_sidebar_statusbar_newEntity").on("click", function(){
			loadSpritesheetUpload(true);
			EditCanvas.stop();
		});
		
		$(document).on("click", "#SpritesheetUpload_btnSave",function(){
			SpritesheetUpload.save(function(){
				loadEditCanvas();
				
				updateGamestate();
			}, function(){});
		});
		
		$(document).on("click", "#SpritesheetUpload_btnDiscard", function(){
			SpritesheetUpload.close();
			loadEditCanvas();
		});
		
		$(document).on("click", ".Creator_sidebarTile", function(){
			var box = $(this).attr("boxId");
			var entity = $(this).attr("entityId");
			if(box){
				Tools.selectedBox = box;
				Tools.selectedEntity = -1;
			} else {
				Tools.selectedBox = -1;
				Tools.selectedEntity = entity;
			}
			$(".Creator_sidebarTile").removeClass("active");
			$(this).addClass("active");
		});
		
	};
	
	this.unload = function(){
		if(EditCanvas) EditCanvas.stop();
	};
	
	var loadScenegraph = function(){
		$(document).on("click", "#Creator_left_scenegraph_bgImage i", function(){
			Tools.visibility.bgImage = updateVisibility(this);
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_background i", function(){
			Tools.visibility.background = updateVisibility(this);
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_center i", function(){
			Tools.visibility.center = updateVisibility(this);
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_foreground i", function(){
			Tools.visibility.foreground = updateVisibility(this);
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_grid i", function(){
			Tools.visibility.grid = updateVisibility(this);
			EditCanvas.redraw();
		});
		
		$(document).on("click", "#Creator_left_scenegraph_background p", function(){
			Tools.selectedLayer = WGC.data.game.backgroundLayerId;
			if($(this).parent().children("i").hasClass("hidden")){
				Tools.visibility.background = updateVisibility($(this).parent().children("i"));
			}
			$(".Creator_left_scenegraph_node").removeClass("active");
			$(this).parent().addClass("active");
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_center p", function(){
			Tools.selectedLayer = WGC.data.game.centerLayerId;
			if($(this).parent().children("i").hasClass("hidden")){
				Tools.visibility.center = updateVisibility($(this).parent().children("i"));
			}
			$(".Creator_left_scenegraph_node").removeClass("active");
			$(this).parent().addClass("active");
			EditCanvas.redraw();
		});
		$(document).on("click", "#Creator_left_scenegraph_foreground p", function(){
			Tools.selectedLayer = WGC.data.game.foregroundLayerId;
			if($(this).parent().children("i").hasClass("hidden")){
				Tools.visibility.foreground = updateVisibility($(this).parent().children("i"));
			}
			$(".Creator_left_scenegraph_node").removeClass("active");
			$(this).parent().addClass("active");
			EditCanvas.redraw();
		});
	};
	
	var updateVisibility = function(icon){
		if($(icon).hasClass("visible")){
			$(icon).removeClass("visible");
			$(icon).addClass("semi");
			return 1;
		} else if($(icon).hasClass("semi")){
			if($(icon).parent().hasClass("active")){
				$(icon).removeClass("semi");
				$(icon).addClass("visible");
				return 2;
			} else {
				$(icon).removeClass("semi");
				$(icon).addClass("hidden");
				$(icon).removeClass("icon-eye-open");
				$(icon).addClass("icon-eye-close");
				return 0;
			}
		} else if($(icon).hasClass("hidden")){
			$(icon).removeClass("hidden");
			$(icon).addClass("visible");
			$(icon).removeClass("icon-eye-close");
			$(icon).addClass("icon-eye-open");
			return 2;
		}
	}

	
	var enableSidebarRescale = function(){
		var MINHEIGHT = 32;
	
		var moving = false;
		var startY = 0;
		var startTopHeight = 0;
		var startBottomHeight = 0;
		
		var mover;
		
		$(".Creator_sidebar_separator").on("mousedown",function(e){
			mover = $(this);
			moving = true;
			startY = e.pageY;
			
			startTopHeight = mover.parent().children(".Creator_sidebar_separable_top").outerHeight();
			startBottomHeight = mover.parent().children(".Creator_sidebar_separable_bottom").outerHeight();
			
			$("body").css("cursor","n-resize");
		});
		
		$("*").on("mousemove",function(e){
			if(moving){
				var dY = e.pageY - startY;
				
				var top = mover.parent().children(".Creator_sidebar_separable_top");
				var bottom = mover.parent().children(".Creator_sidebar_separable_bottom");
				
				if(startTopHeight + dY >= MINHEIGHT && startBottomHeight - dY >= MINHEIGHT){
					top.css("height", (startTopHeight + dY) + "px");
					bottom.css("height", (startBottomHeight - dY) + "px");
				}
			}
		});
		
		$("*").on("mouseup",function(e){
			moving = false;
			$("body").css("cursor","auto");
		});
	};
	
	var loadEditCanvas = function(){
		H5.ScriptLoader.include("content/element/EditCanvas.js?v=" + H5.getRandom(), function(){
	
			$("#Creator_center").setTemplateURL("content/element/EditCanvas.html?v=" + H5.getRandom(), null, {runnable_functions: true}).processTemplate({
				btnStart: H5.t("Creator_btnStart"),
				btnDebug: H5.t("Creator_btnDebug"),
				selectionTool: H5.t("Creator_selectionTool"),
				drawTool: H5.t("Creator_drawTool"),
				drawToolArea: H5.t("Creator_drawToolArea"),
				eraseTool: H5.t("Creator_eraseTool"),
				eraseToolArea: H5.t("Creator_eraseToolArea"),
				onload: function(){
					window.setTimeout(function(){
						EditCanvas.init();
					}, 1);
				},
			});
		
		});
	};
	
	var loadPlayCanvas = function(debug){
		H5.ScriptLoader.include("content/element/PlayCanvas.js?v=" + H5.getRandom(), function(){
	
			$("#Creator_center").setTemplateURL("content/element/PlayCanvas.html?v=" + H5.getRandom(), null, {runnable_functions: true}).processTemplate({
				btnStop: H5.t("Creator_btnStop"),
				onload: function(){
					window.setTimeout(function(){
						PlayCanvas.init(debug);
					}, 1);
				},
			});
		
		});
	};
	
	var loadSpritesheetUpload = function(isEntity){
		H5.ScriptLoader.include("content/element/SpritesheetUpload.js?v=" + H5.getRandom(), function(){
	
			$("#Creator_center").setTemplateURL("content/element/SpritesheetUpload.html?v=" + H5.getRandom(), null, {runnable_functions: true}).processTemplate({
				btnSave: H5.t("Creator_btnSave"),
				btnDiscard: H5.t("Creator_btnDiscard"),
				btnPrevious: H5.t("SpriteSheetUpload_btnPrevious"),
				btnMoveBack: H5.t("SpriteSheetUpload_btnMoveBack"),
				btnDuplicate: H5.t("SpriteSheetUpload_btnDuplicate"),
				btnRemove: H5.t("SpriteSheetUpload_btnRemove"),
				btnMoveForward: H5.t("SpriteSheetUpload_btnMoveForward"),
				btnNext: H5.t("SpriteSheetUpload_btnNext"),
				btnInvert: H5.t("SpriteSheetUpload_btnInvert"),
				preview: H5.t("SpriteSheetUpload_preview"),
				filename: H5.t("SpriteSheetUpload_filename"),
				name: H5.t("SpriteSheetUpload_name"),
				fps: H5.t("SpriteSheetUpload_fps"),
				drop: H5.t("SpriteSheetUpload_drop"),
				onload: function(){
					window.setTimeout(function(){
						SpritesheetUpload.init(isEntity);
					}, 1);
				},
			});
		
		});
	};
	
	var updateGamestate = function(){
	
		$("#Creator_right_boxes").html("");
		$("#Creator_right_entities").html("");
	
				
		for(var i = 0; i < WGC.data.box.length; i++){
			if(WGC.data.box[i]){
				$("#Creator_right_boxes").append("<div class=\"Creator_sidebarTile " + ((Tools.selectedBox == WGC.data.box[i].boxId)? "active" : "") + "\" boxId=\""+ WGC.data.box[i].boxId + "\" style=\"background-image: url(" + WGC.data.sprite[WGC.data.box[i].spriteId].image + ");\"></div>");
			}
		}
		
		for(var i = 0; i < WGC.data.entity.length; i++){
			if(WGC.data.entity[i]){
				var es = WGC.data.entitystate[WGC.data.entity[i].defaultStateId];
				if(es){
				
					var width = WGC.data.sprite[es.spriteId].width;
					var height = WGC.data.sprite[es.spriteId].height;
				
					$("#Creator_right_entities").append("<div class=\"Creator_sidebarTile " + ((Tools.selectedEntity == WGC.data.entity[i].entityId)? "active" : "") + "\" entityId=\""+ WGC.data.entity[i].entityId + "\" style=\"background-image: url(" + WGC.data.sprite[es.spriteId].image + "); width: " + width + "px; height: " + height + "px;\"></div>");
					
				}
			}
		}
				
	};
};